package com.noble.glamour.ui.start;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.noble.glamour.R;
import com.noble.glamour.module.local.album.ImgSelectUtil;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.library.base.BaseAppCompatActivity;
import com.noble.glamour.module.logic.baseui.LoadingDialog;
import com.noble.glamour.module.logic.request.HttpResponse;
import com.noble.glamour.module.logic.request.RequestComplete;
import com.noble.glamour.module.util.UIShow;
import com.noble.library.third.ThirdConstant;
import com.noble.glamour.ui.utils.NoDoubleClickListener;
import com.noble.library.image.ImageLoader;
import com.noble.library.log.PToast;
import com.noble.library.observe.MsgMgr;
import com.noble.library.observe.MsgType;
import com.noble.library.utils.FileUtil;

import org.json.JSONObject;

/**
 * 注册资料填写页
 */
public class RegInfoAct extends BaseAppCompatActivity implements ImgSelectUtil.OnChooseCompleteListener {
    private ImageView user_head, reg_man, reg_female;
    private EditText nickname;

    private String avatarLink = "";
    private boolean ifUpHead = true;    // 是否已设置头像
    private int gender = 1;             // 默认男性用户

    private int loginType;           // 登录方式  ThirdConstant.NORMAL_LOGIN ：手机验证码(默认)  ThirdConstant.WECHAT : 微信登录
    private String mobile = "";      // 手机号注册
    private String openid = "";      // 微信注册

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reg_info_act);
        initView();
    }

    private void initView() {
        loginType = getIntent().getIntExtra("loginType", ThirdConstant.NORMAL_LOGIN);
        if (loginType == ThirdConstant.WECHAT)
            openid = getIntent().getStringExtra("openid");
        else
            mobile = getIntent().getStringExtra("mobile");
        user_head = (ImageView) findViewById(R.id.user_head);
        reg_man = (ImageView) findViewById(R.id.reg_man);
        reg_female = (ImageView) findViewById(R.id.reg_female);
        nickname = (EditText) findViewById(R.id.reg_name);

        reg_man.setSelected(true);
        user_head.setOnClickListener(listener);
        reg_man.setOnClickListener(listener);
        reg_female.setOnClickListener(listener);
        findViewById(R.id.reg_protocol).setOnClickListener(listener);
        findViewById(R.id.login_btn).setOnClickListener(listener);
    }

    private NoDoubleClickListener listener = new NoDoubleClickListener() {
        @Override
        public void onNoDoubleClick(View v) {
            switch (v.getId()) {
                case R.id.user_head:
                    ImgSelectUtil.getInstance().pickPhoto(RegInfoAct.this, RegInfoAct.this);
                    break;

                case R.id.reg_man:
                    gender = 1;
                    reg_man.setSelected(true);
                    reg_female.setSelected(false);
                    break;

                case R.id.reg_female:
                    gender = 2;
                    reg_man.setSelected(false);
                    reg_female.setSelected(true);
                    break;

                case R.id.login_btn:
                    login();
                    break;

                case R.id.reg_protocol:
                    UIShow.showRegisterAgree(RegInfoAct.this);
                    break;
            }
        }
    };

    private void login() {
        String name = nickname.getText().toString();
        if (TextUtils.isEmpty(name)) {
            PToast.showShort(getString(R.string.toast_nickname_isnull));
            return;
        }
        LoadingDialog.show(this, getString(R.string.loading_reg));
        // TODO 做修改个人资料请求
        //ModuleMgr.getLoginMgr().onRegister(this, name, gender, mobile, openid);
    }

    @Override
    public void onComplete(String... path) {
        if (path == null || path.length == 0 || TextUtils.isEmpty(path[0])) {
            return;
        }
        if (FileUtil.isExist(path[0])) {
            LoadingDialog.show(this, getResources().getString(R.string.user_info_avatar_upload));
            ModuleMgr.getCenterMgr().uploadAvatar(path[0], new RequestComplete() {
                @Override
                public void onRequestComplete(HttpResponse response) {
                    if (response.isOk()) {
                        LoadingDialog.closeLoadingDialog();
                        if (response.isOk()) {
                            JSONObject jsonObject = response.getResponseJson();
                            String file_path = jsonObject.optString("file_path");
                            avatarLink = file_path;
                            ImageLoader.loadCircleAvatar(RegInfoAct.this, file_path, user_head);
                            MsgMgr.getInstance().sendMsg(MsgType.MT_Update_MyInfo, null);
                            ifUpHead = true;
                        } else {
                            PToast.showShort(getResources().getString(R.string.toast_head_error));
                        }
                    }
                }
            });
        } else {
            PToast.showShort(getResources().getString(R.string.toast_picpath_error));
        }
    }

    @Override
    protected boolean isImmersionBar() {
        return false;
    }
}
